<?php
include("template_top.php");
if(!isset($_POST['action'])){
	$_SESSION['items'] = NULL;
?>
<h1>Hardware and Software Loan Center</h1>
<table width="100%">
	<tr>
    	<td width="50%">
        <h4>Check-In Hardware/Software</h4>
		<center>    
		<?php if($_POST['CheckIn'] == 'CheckIn'){ ?>
    		<?php
			$sql = "SELECT * FROM `tbLoanReceipts` WHERE `sReceiptId` = :id";
			$q = db::Query($sql, array(':id'=>$_POST['id']));
			$row = $q->fetch();
			
			if($row == ''){
				echo "Receipt ID Does Not Exist!<br/><br/><a href=''>Try Again</a>";
			}elseif($row['sStatus'] == 'Returned'){
				echo "Receipt ID Already Checked In!<br/><br/><a href=''>Go Back</a>";
			}else{
				$sql = "UPDATE `tbLoanReceipts` SET `sStatus` = 'Returned' WHERE `sReceiptId` = :id";
				db::Query($sql, array(':id'=>$_POST['id']));
				//$sql = "DELETE FROM `tbLoanLink` WHERE `sReceiptId` = :id";
				//db::Query($sql, array(':id'=>$_POST['id']));
				echo "Item(s) Checked In!<br/><br/><a href=''>Check In Another</a>";
			}
			?>
		<?php }else{ ?>
	        <form action="Hardware_Software_Center.php" method="POST">
				Receipt ID Number:<br/>
		        <input type="text" name="id" /><br/>
            	<input type="hidden" name="CheckIn" value="CheckIn" />
    	    	<input type="submit" value="Check-In" />
	        </form>
        <?php } ?>
	    </center>
        </td>
        <td>
        <h4>Check-Out Hardware/Software</h4>
        <center>
        <form action="Hardware_Software_Center.php" name="form" method="POST">
        To Begin, Please Enter The Customers Banner ID:<br/>
	        <input type="text" name="banner" id="banner" value="@" /><br/>
            <input type="hidden" name="action" value="Step1" />
        	<input type="button" value="Continue" onclick="if(document.getElementById('banner').value == '@'){alert('A Banner ID is Required.');}else{document.form.submit();}" />
        </form>
        </center>
        </td>
	</tr>
</table>
<?php }else{ ?>
	<?php if($_POST['action'] == 'Step1'){ ?>
    <?php
	$sql = "SELECT * FROM SPRIDEN, GOBTPAC WHERE SPRIDEN_PIDM = GOBTPAC_PIDM AND SPRIDEN_ID = :uname AND SPRIDEN_CHANGE_IND is null";
	$q = Oracle::Query($sql, array(':uname'=>$_POST['banner']));
	$row = $q->fetch();
	if($row['SPRIDEN_ID'] != $_POST['banner']){
		echo "That Banner ID Was Not Valid!<br/><br/><a href=''>Try Again</a>";
		exit;
	}
	?>
    <h1>Check Out Hardware/Software For <?php echo $row['SPRIDEN_FIRST_NAME']." ".$row['SPRIDEN_LAST_NAME'] ?></h1>
        <form action="Hardware_Software_Center.php" name="form" method="POST">
		<table width="100%">
        	<tr>
            	<td width="180">
                	Office Phone Number:
                </td>
                <td>
                	<input type="text" name="number" id="number" value="" style="width:90%" />
                </td>
			</tr>
        	<tr>
            	<td>
                	Campus:
                </td>
                <td>
                	<input type="text" name="campus" id="campus" value="" style="width:90%" />
                </td>
			</tr>
        	<tr>
            	<td>
                	Department/Division:
                </td>
                <td>
                	<input type="text" name="department" id="department" value="" style="width:90%" />
                </td>
			</tr>
        	<tr>
            	<td>
                	Expected Return Date:
                </td>
                <td>
                	<input type="text" name="return" id="return" value="" style="width:90%" />
                </td>
			</tr>
        	<tr>
            	<td>
                	Academic/Business Purpose:
                </td>
                <td>
                	<textarea name="purpose" id="purpose" style="width:90%" rows="3"></textarea>
                </td>
			</tr>
            <tr>
            	<td><input type="hidden" name="action" value="Step2"><input type="hidden" name="banner" value="<?php echo $_POST['banner'] ?>"></td>
                <td><input type="button" value="Continue" onClick="if(document.getElementById('number').value == '' || document.getElementById('campus').value == '' || document.getElementById('department').value == '' || document.getElementById('return').value == '' || document.getElementById('purpose').value == ''){alert('All Text Fields Are Required!');}else{document.form.submit();}" /></td>
            </tr>
		</table>
        </form>
	<?php } ?>
	<?php if($_POST['action'] == 'Step2' or $_POST['action'] == 'Step3'){ ?>
	    <?php
		$sql = "SELECT * FROM SPRIDEN, GOBTPAC WHERE SPRIDEN_PIDM = GOBTPAC_PIDM AND SPRIDEN_ID = :uname AND SPRIDEN_CHANGE_IND is null";
		$q = Oracle::Query($sql, array(':uname'=>$_POST['banner']));
		$row = $q->fetch();
		if($_SESSION['items'] == NULL){
			$_SESSION['items'] = array();
		}
		if($_POST['action'] == 'Step3'){
			$con = true;
			foreach($_SESSION['items'] as $value){
				if($value == $_POST['item']){
					$con = false;
				}
			}
			if($con){
				$sql = "SELECT * FROM `tbLoanLink` WHERE `sItemIdentifier` = :id";
				$q = db::Query($sql, array(':id'=>$_POST['item']));
				while($row2 = $q->fetch()){
					$sql = "SELECT * FROM `tbLoanReceipts` WHERE `sReceiptId` = :id";
					$q2 = db::Query($sql, array(':id'=>$row2['sReceiptId']));
					$rw = $q2->fetch();
					if($rw['sStatus'] == 'Awaiting Return'){
						$con = false;
					}
				}
				if($con){
					$sql = "SELECT * FROM `tbHardware` WHERE `iItemID` = :id";
					$q = db::Query($sql, array(':id'=>$_POST['item']));
					$row2 = $q->fetch();
					if($row2['iItemID'] == $_POST['item']){
						$_SESSION['items'][] = $_POST['item'];
					}else{
						$sql = "SELECT * FROM `tbSoftware` WHERE `iItemID` = :id";
						$q = db::Query($sql, array(':id'=>$_POST['item']));
							$row2 = $q->fetch();
						if($row2['iItemID'] == $_POST['item']){
								$_SESSION['items'][] = $_POST['item'];
						}else{
							?>
                    		<script>alert('Item Not Found! Contact Kody!')</script>
                    		<?php
						}
					}
				}else{
					?>
                    <script>alert('Item Already Checked-Out')</script>
                    <?php
				}
			}else{
				?>
                <script>alert('Item Already Added To Check-Out List')</script>
                <?php
			}
		}
		?>
	    <h1>Check Out Hardware/Software For <?php echo $row['SPRIDEN_FIRST_NAME']." ".$row['SPRIDEN_LAST_NAME'] ?></h1>
        <center>
        <form action="Hardware_Software_Center.php" method="POST">
        Item Barcode:<br/>
        <input type="text" name="item"><br/>
        <input type="hidden" name="action" value="Step3">
        <?php echo "<input type='hidden' name='return' value='".$_POST['return']."'><input type='hidden' name='purpose' value='".$_POST['purpose']."'><input type='hidden' name='department' value='".$_POST['department']."'><input type='hidden' name='campus' value='".$_POST['campus']."'><input type='hidden' name='number' value='".$_POST['number']."'>" ?>
        <input type="hidden" name="banner" value="<?php echo $_POST['banner'] ?>">
        <input type="submit" value="Add">
        </form>
        </center><br/>
        <h4>Items</h4>
        <div id="items" style="padding-left:15px;">
        <?php
		if(count($_SESSION['items']) == 0){
		?>
	        No Items Checked Out Yet...
        </div>
        <?php
		}else{
			$first = true;
			foreach($_SESSION['items'] as $value){
				$sql = "SELECT * FROM `tbHardware` WHERE `iItemID` = :id ORDER BY `sMain` DESC";
				$q = db::Query($sql, array(':id'=>$value));
				$row = $q->fetch();
				if($row['iItemID'] != $value){
					$sql = "SELECT * FROM `tbSoftware` WHERE `iItemID` = :id";
					$q = db::Query($sql, array(':id'=>$value));
					$row = $q->fetch();
					if(!$first){
						echo "<br/><br/>";
					}
					echo "<strong>".$row['sDescription']."</strong>";
				}else{
					$q = db::Query($sql, array(':id'=>$value));
					while($row = $q->fetch()){
						if($row['sMain'] == 'Y'){
							if(!$first){
								echo "<br/><br/>";
							}
							echo "<strong>".$row['sDescription']."</strong>";
						}else{
							echo "<br/><span style='padding-left:5px'>".$row['sDescription']."</span>";
						}
					}
				}
				$first = false;
			}
		echo "<br/><br/><form action='Hardware_Software_Center_Receipt.php' method='POST'><input type='hidden' name='banner' value='".$_POST['banner']."'><input type='hidden' name='return' value='".$_POST['return']."'><input type='hidden' name='purpose' value='".$_POST['purpose']."'><input type='hidden' name='department' value='".$_POST['department']."'><input type='hidden' name='campus' value='".$_POST['campus']."'><input type='hidden' name='number' value='".$_POST['number']."'><input type='submit' value='Check-Out and Print Receipt' style='width:100%; height:40px;'></form></div>";
		}
		?>
    <?php } ?>
<?php } ?>
<br/>
<h4>Current Items Out</h4>
<table width="100%">
	<tr>
    	<td style="font-weight:bold; border-bottom:solid 1px #666666">Receipt ID</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Last Name, First Name</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Loan Type</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666" width="130">Loan Status</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Date Loaned</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Loaned By</td>
    </tr>
    <?php
	$sql = "SELECT * FROM `tbLoanReceipts` WHERE `sStatus` != 'Returned'";
	$q = db::Query($sql);
	while($row = $q->fetch()){ ?>
    <tr>
    	<td style="border-bottom:solid 1px #666666">
        <a href="Hardware_Software_Center_Receipt_Reprint.php?id=<?php echo $row['sReceiptId'] ?>" target="_blank"><?php echo $row['sReceiptId'] ?></a>
        </td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sLname'].", ".$row['sFname'] ?></td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sType'] ?></td>
        <td style="border-bottom:solid 1px #666666" width="130">
		<?php if($row['sStatus'] == 'Awaiting Return'){ ?>
		<a href="Hardware_Software_Center.php?re=<?php echo $row['sReceiptId'] ?>" onmouseover="<?php if($row['sEmail'] != ''){ ?>document.getElementById('<?php echo $row['sReceiptId'] ?>').innerHTML = 'Send Request Email';<?php }else{ ?>document.getElementById('<?php echo $row['sReceiptId'] ?>').innerHTML = 'No E-Mail Found';<?php } ?>" onmouseout="document.getElementById('<?php echo $row['sReceiptId'] ?>').innerHTML = '<?php echo $row['sStatus'] ?>';">
		<span id="<?php echo $row['sReceiptId'] ?>"><?php echo $row['sStatus'] ?></span>
        </a>
        <?php }else{ ?>
		<?php echo $row['sStatus'] ?>
        <?php } ?>
        </td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sDate']." @ ".$row['sTime'] ?></td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sUname'] ?></td>
    </tr>
    <?php } ?>
</table>
<br/>
<h4>Items Returned</h4>
<table width="100%">
	<tr>
    	<td style="font-weight:bold; border-bottom:solid 1px #666666">Receipt ID</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Last Name, First Name</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Loan Type</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666" width="130">Loan Status</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Date Loaned</td>
        <td style="font-weight:bold; border-bottom:solid 1px #666666">Loaned By</td>
    </tr>
    <?php
	$sql = "SELECT * FROM `tbLoanReceipts` WHERE `sStatus` = 'Returned'";
	$q = db::Query($sql);
	while($row = $q->fetch()){ ?>
    <tr>
    	<td style="border-bottom:solid 1px #666666">
        <a href="Hardware_Software_Center_Receipt_Reprint.php?id=<?php echo $row['sReceiptId'] ?>" target="_blank"><?php echo $row['sReceiptId'] ?></a>
        </td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sLname'].", ".$row['sFname'] ?></td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sType'] ?></td>
        <td style="border-bottom:solid 1px #666666" width="130">
		<?php if($row['sStatus'] == 'Awaiting Return'){ ?>
		<a href="Hardware_Software_Center.php?re=<?php echo $row['sReceiptId'] ?>" onmouseover="<?php if($row['sEmail'] != ''){ ?>document.getElementById('<?php echo $row['sReceiptId'] ?>').innerHTML = 'Send Request Email';<?php }else{ ?>document.getElementById('<?php echo $row['sReceiptId'] ?>').innerHTML = 'No E-Mail Found';<?php } ?>" onmouseout="document.getElementById('<?php echo $row['sReceiptId'] ?>').innerHTML = '<?php echo $row['sStatus'] ?>';">
		<span id="<?php echo $row['sReceiptId'] ?>"><?php echo $row['sStatus'] ?></span>
        </a>
        <?php }else{ ?>
		<?php echo $row['sStatus'] ?>
        <?php } ?>
        </td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sDate']." @ ".$row['sTime'] ?></td>
        <td style="border-bottom:solid 1px #666666"><?php echo $row['sUname'] ?></td>
    </tr>
    <?php } ?>
</table>
<?php
include("template_bot.php");
?>